package com.china08.yxyapi.api.home.kecheng.xiaoben;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;

/**
 * 校本课程--列表页--一级分类名称
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengXiaobenOneController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSchoolCatalogRepository yxyLessonsSchoolCatalogRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/xiaoben/one/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response) {

        String schoolId = getProfile(request, response).getSchoolId();

        List<RespModel> returnLst = new ArrayList<>();
        RespModel aa = new RespModel();
        aa.idOne = "";
        aa.nameOne = "不限";
        aa.schoolIdOne = schoolId;
        returnLst.add(aa);

        List<YxyLessonsSchoolCatalog> yxyLessonsSchoolCatalogLst = yxyLessonsSchoolCatalogRepository
                .findBySchoolIdAndLevelAll(schoolId, 1);
        for (YxyLessonsSchoolCatalog yxyLessonsSchoolCatalog : yxyLessonsSchoolCatalogLst) {
            RespModel model = new RespModel();

            // 一级参数
            String id1 = yxyLessonsSchoolCatalog.getId();
            model.idOne = yxyLessonsSchoolCatalog.getId();
            model.nameOne = yxyLessonsSchoolCatalog.getName();
            model.schoolIdOne = yxyLessonsSchoolCatalog.getSchoolId();

            List<CatalogTwo> catalogTwoList = new ArrayList<CatalogTwo>();
            List<YxyLessonsSchoolCatalog> yxyLessonsSchoolCatalogLst1 = yxyLessonsSchoolCatalogRepository
                    .findBySchoolIdAndLevelAndPidAll(schoolId, 2, id1);
            for (YxyLessonsSchoolCatalog yxyLessonsSchoolCatalog2 : yxyLessonsSchoolCatalogLst1) {
                CatalogTwo catalogTwo = new CatalogTwo();

                // 二级参数
                catalogTwo.idTwo = yxyLessonsSchoolCatalog2.getId();
                catalogTwo.nameTwo = yxyLessonsSchoolCatalog2.getName();
                catalogTwo.schoolIdTwo = yxyLessonsSchoolCatalog2.getSchoolId();
                catalogTwoList.add(catalogTwo);
            }
            model.catalogTwoList = catalogTwoList;
            returnLst.add(model);
        }
        return returnLst;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        public String nameTwo;

        public String idTwo;

        // 一级学校ID
        public String schoolIdOne;

        // 一级目录名称
        public String nameOne;

        // 一级目录Id
        public String idOne;

        public List<CatalogTwo> catalogTwoList;
    }

    public static class CatalogTwo {

        // 二级学校ID
        public String schoolIdTwo;

        // 二级目录ID
        public String idTwo;

        // 二级目录名称
        public String nameTwo;

    }

}
